jQuery(function($) {
    var doc = document,
        $doc = $(doc),
        M = {},
       
        timer = 0;


    //显示表单框
    $doc.on("click", "button", function() {
        $(".pop").fadeIn();
    });
    //隐藏表单框
    $doc.on("click", ".clear", function() {
        $(".pop").fadeOut();
    });

    /*表单验证 -start-*/
    var need = {
        name: false,
        area: false,
        phone: false,
        qq: false,
        period: false,
        desc: false
    };

    function sendErrMessage(node, msg) {
        utils.errorMessage(node, msg || "*");
    }
    function clearErrMessage(input, node) {
        $(doc).on("focus", input, function() {
            utils.clearErrMessage(node);
        });
    }
    function empty(val, key, err, msg) {
        if (!utils.empty(val)) {
            need[key] = true;
        } else {
            need[key] = false;
            sendErrMessage(err, msg);
        }
    }
    function checkPeriod(period) {
        if (!utils.empty(period)) {
            need.period = true;
        } else {
            need.period = false;
            sendErrMessage($(".period_error"));
            return;
        }

        if (utils.isNumber(period)) {
            need.period = true;
        } else {
            need.period = false;
            sendErrMessage($(".period_error"), "必须是数值！");
        }
    }
    //提交表单
    function submitForm() {
        var realName = $(".real_name input").val(),
            area = $(".area input").val(),
            phone = $(".phone input").val(),
            qq = $(".qq input").val(),
            period = $(".period input").val(),
            desc = $(".desc textarea").val();


        empty(realName, "name", $(".name_error"));
        empty(area, "area", $(".area_error"));
        empty(phone, "phone", $(".phone_error"));
        empty(qq, "qq", $(".qq_error"));
        empty(desc, "desc", $(".desc_error"));
        // empty(period, "period", $(".period_error"));
        checkPeriod(period);

    if (need.name &&
        need.area &&
        need.area &&
        need.phone &&
        need.qq &&
        need.period &&
        need.desc) {
        //do something
        console.log(1111);
    }
    };

    $doc.on("click", ".pop button", function() {
        submitForm();
    });
    //提交表单 -end-

    //清除错误信息
    clearErrMessage(".real_name", $(".name_error"));
    clearErrMessage(".area input", $(".area_error"));
    clearErrMessage(".phone input", $(".phone_error"));
    clearErrMessage(".qq input", $(".qq_error"));
    clearErrMessage(".period input", $(".period_error"));
    clearErrMessage(".desc textarea", $(".desc_error"));
    /*表单验证 -end-*/


    //动画交互 -start-

    var TIME = {
            timer: 0,
            autoTimer: 0
        };
    function create(elm) {
        return doc.createElement(elm);
    }

    function getViewport() {
        if (doc.compatMode == "BackCompat") {
            return {
                width: doc.body.clientWidth,
                height: doc.body.clientHeight
            };
        } else {
            return {
                width: doc.documentElement.clientWidth,
                height: doc.documentElement.clientHeight
            };
        }
    }

    M.FullScreen = function() {
        var that = this;
        that.banner = doc.querySelectorAll(".banner");
        that.nav = doc.querySelector(".nav");
        that.slideBox = doc.querySelector(".slideBox");
        that.gallery = doc.querySelector(".gallery");
        that.bannerHeight = 0;
        that.screenHeight = getViewport().height;
        this.index = 0;
    };

    fs = M.fullScreen = new  M.FullScreen();

    fs.setIndex = function(lis) {
        var len = lis.length,
            i = 0;

        for (i; i < len; i++) {
            lis[i].index = i;
        }
    };

    fs.setElementHeight = function() {
        var that = this,
            banner = that.banner,
            len = banner.length,
            screenHeight = getViewport().height,
            fragment = doc.createDocumentFragment(),
            li = null;

        for(var i = 0; i < len; i++) {
            banner[i].style.height = screenHeight + "px";
            fragment.appendChild(create("li"));
        }

        that.slideBox.style.height = screenHeight + "px";
        that.nav.appendChild(fragment);
        that.nav.firstChild.className = "active";
        that.bannerHeight = that.banner[0].offsetHeight;

        fs.setIndex(doc.querySelectorAll(".nav li"));
    };
    fs.setElementHeight();

    fs.resetElementHeight = function() {
        var banner = this.banner,
            len = banner.length,
            screenHeight = getViewport().height;

        clearTimeout(timer);
        timer = setTimeout(function() {
            for(var i = 0; i < len; i++) {
                banner[i].style.height = screenHeight + "px";
            }

            fs.slideBox.style.height = screenHeight + "px";
            fs.bannerHeight = fs.banner[0].offsetHeight;
            fs.movePage(fs.index);
        }, 500);
    };
    
    fs.movePage = function(index) {
        var height =  index * (-this.bannerHeight);
        this.gallery.style.top = height + "px";
    };

    fs.resetNav = function(index) {
        var lis = doc.querySelectorAll(".nav li"),
            len = lis.length;

        for (var i = 0; i < len; i++) {
            lis[i].className = "";
        }

        lis[index].className = "active";
    };

    fs.nav.addEventListener("click", function(event) {
        var target = event.target;
        if (target.tagName === "LI") {
            var index = target.index;
            fs.movePage(index);
            fs.index = index;
            fs.resetNav(index);
        }
    }, false);

    fs.autoPlay = function() {
        clearInterval(TIME.autoTimer);
        TIME.autoTimer = setInterval(function() {
            var len = fs.gallery.children.length,
                index = fs.index;

            fs.index++;
            if (fs.index >= len) {
                fs.index = 0;
            }

            fs.movePage(index);
            fs.resetNav(index);
        }, 4000);
    };fs.autoPlay();
    
    fs.mouseWheel = function(detail) {
        var len = this.gallery.children.length;

        switch(detail) {
            case 120:
                fs.index++;
                if (fs.index >= len) {
                    fs.index = len -1;
                }
                break;
            case -120:
                fs.index--;
                if (fs.index < 0) {
                    fs.index = 0;
                }
                break;
        }
        fs.movePage(fs.index);
        fs.resetNav(fs.index);
    };

    fs.keyPress = function(keycode) {
        var len = this.gallery.children.length;

        switch(keycode) {
            case 38:
                fs.index++;
                if (fs.index >= len) {
                    fs.index = len-1;
                }
               
                break;
            case 40:
                fs.index--;
                if (fs.index <= 0) {
                    fs.index = 0;
                }
                
                break;
        }
        fs.movePage(fs.index);
        fs.resetNav(fs.index);
    };

    //事件
    doc.addEventListener("mousewheel", function(event) {
        fs.mouseWheel(event.wheelDelta || event.detail * 40);
    }, false);

    doc.addEventListener("DOMMouseScroll", function(event) {
        fs.mouseWheel(event.wheelDelta || event.detail * 40);
    }, false);

    doc.addEventListener("keyup", function(event) {
        fs.keyPress(event.keyCode);
    }, false);

    fs.slideBox.addEventListener("mouseover", function(event) {
        clearInterval(TIME.autoTimer);
    }, false);

    fs.slideBox.addEventListener("mouseout", function() {
        fs.autoPlay();
    }, false);

    window.addEventListener("resize", function() {
        fs.resetElementHeight();
    }, false);
     //动画交互 -end-

});